package com.example.taobao.ui.dashboard;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.taobao.R;
import com.example.taobao.databinding.FragmentDashboardBinding;
import com.example.taobao.socketActivity;

public class DashboardFragment extends Fragment {

    private DashboardViewModel dashboardViewModel;
    private FragmentDashboardBinding binding;

    private Button chooseBut,BianjiBut,YanBut;
    private TextView zSTextView1;
    private EditText dashEditText;
    private ImageView dashImage;
    final int FLAG_MSG = 0x001;
    int[] flag= new int[]{R.drawable.gb1,R.drawable.gb2,R.drawable.gb3};
    private Message message;
    private Animation[] animation = new Animation[2];
    private ViewFlipper Flipper;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
//        View root = binding.getRoot();
        View view = inflater.inflate(R.layout.fragment_dashboard,null);

        chooseBut =view.findViewById(R.id.chooseBut);
        BianjiBut = view.findViewById(R.id.BianXie);
        zSTextView1 = view.findViewById(R.id.dashTxt);

        dashEditText = view.findViewById(R.id.nrEdt);

        dashImage = view.findViewById(R.id.dashImag);

        Flipper = view.findViewById(R.id.FenViewFlipper);

        YanBut = view.findViewById(R.id.ShaChuaTxt);



        chooseBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, 295);

            }
        });

        YanBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), socketActivity.class);
                startActivityForResult(intent,456);
            }
        });

        BianjiBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                zSTextView1.setText(dashEditText.getText().toString());
            }
        });

        for (int i = 0; i < flag.length; i++) {

            ImageView imageView = new ImageView(getActivity());
            imageView.setImageResource(flag[i]);
            Flipper.addView(imageView);
        }

        animation[0] = AnimationUtils.loadAnimation(getActivity(), R.anim.left_slide);
        animation[1] = AnimationUtils.loadAnimation(getActivity(), R.anim.right_slide);
        Flipper.setInAnimation(animation[1]);   //为flipper设置图片进入动画效果
        Flipper.setOutAnimation(animation[0]);  //为flipper设置图片退出动画效果

        message = Message.obtain();       //获得消息对象
        message.what = FLAG_MSG;  //设置消息代码
        handler.sendMessage(message);



//        final TextView textView = binding.textDashboard;
        dashboardViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
//                textView.setText(s);
            }
        });
        return view;
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == 295) {
            // 从相册返回的数据
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                dashImage.setImageURI(uri);
            }
        }
    }

    Handler handler = new Handler() {  //创建android.os.Handler对象
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == FLAG_MSG) {  //如果接收到的是发送的标记消息
                Flipper.showNext();                  //示下一张图片show
            }
            message = handler.obtainMessage(FLAG_MSG);   //获取要发送的消息
            handler.sendMessageDelayed(message, 3000);  //延迟3秒发送消息
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}